﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using System.Drawing;
using Silvermoon.OpenGL;
using Silvermoon.Shapes;
using Silvermoon.Animations;
using Silvermoon.UI;
using System.Diagnostics;

namespace Testing
{
    public class TextboxDemo : DemoWindowBase
    {
        #region const

        public const string lorem =
             "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris pulvinar venenatis quam, vitae tristique purus ultricies sed. Donec lorem dui, iaculis sed consectetur ac, malesuada in tortor. Donec ac ultricies metus. Pellentesque volutpat consectetur pellentesque. Sed pharetra magna in risus hendrerit vitae congue felis tristique. Nullam vel dui arcu, in placerat magna. Pellentesque in massa pharetra nulla facilisis sodales. Aliquam erat volutpat. Suspendisse eget est mi, ac ultricies lorem. Vivamus ut dolor tellus. Pellentesque ac posuere purus. " +
                     "\n\n Etiam risus leo, posuere a pretium eget, consectetur ac felis. Nam et tellus enim. Nullam consectetur, diam ut malesuada tempus, tortor lacus pulvinar ligula, quis molestie quam sem vel mi. Morbi rutrum magna at libero rutrum nec posuere mauris cursus. Duis imperdiet lacinia sem. Vivamus tempus mauris non quam commodo ut hendrerit nunc interdum. Suspendisse turpis nibh, tincidunt et vulputate non, fermentum nec arcu. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Mauris imperdiet ipsum magna, non bibendum lacus. Fusce vitae sem leo, non faucibus purus. In sagittis massa in turpis sollicitudin sollicitudin. Praesent commodo augue nibh. Ut in magna in massa dignissim bibendum nec molestie quam." +
                     "\n\nMorbi a mauris risus. Sed facilisis ligula urna, at tempor mauris. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec et massa in tellus pellentesque tincidunt. Integer tortor turpis, dignissim non porttitor et, accumsan vel purus. Maecenas pulvinar convallis semper. Vestibulum malesuada augue nec risus dapibus eget sodales velit fringilla. Pellentesque vel nibh feugiat massa sagittis aliquam in a tellus. Sed vitae sem ante. Nam ornare dictum elit at mattis. Aliquam quis ante mi. In at purus eu ligula commodo mollis. Suspendisse sed dui magna. Nunc aliquet sagittis lacus, at luctus mauris commodo non. Mauris consequat auctor dui id mollis. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;" +
                     "\n\nEnd of text.\n";

        public const string copyPaste =
            "Touch and Hold textbox to set the cursor and open tooltip to select.\n" +
            "Choose select to select the word unter the cursor.\n" +
            "Modify the selected text by dragging one of it's anchor:\n" +
            "Touch close to one anchor (round marker) and then drag to the desired location, " +
            "Touch outside an anchor and drag to scroll the content.\n\n" +
            "Tap to unselect.\n\n\n" +
            "Dragging the cursor:\n" +
            "Touch close to the cursor and drag to the desired position.\n\n";

        const int scrollbarWidth = VScrollBar.ScrollbarWidth + 2;

        #endregion

        private TextBox textBox;

        public TextboxDemo()
            : base()
        {
            SetTransition(320, TransitionMask.Zoom | TransitionMask.Fade, 450, TransitionMask.Explode | TransitionMask.Fade);

            ScrollPanel panel = new ScrollPanel
            {
                Margin = new Margin(8, 64, 0, 8),
                Background = Color.Black
            };

            TextBox single1 = new TextBox
            {
                Margin = new Margin(0, 2, 0 + scrollbarWidth, null),
                Height = 64,
                Text = "Single Line"
            };

            TextBox single2 = new TextBox
            {
                Margin = new Margin(0, null, 0 + scrollbarWidth, null),
                Y = 320,
                Height = 64,
                Text = "Bottom Line"
            };


            TextBox mlTextBox = new TextBox
            {
                AcceptReturn = true,
                AutoHeight = true,
                Margin = new Margin(0, 80, 0 + scrollbarWidth, null),
            };
            textBox = mlTextBox;

            mlTextBox.SizeChanged += (s, e) =>
                {
                    single2.Y = mlTextBox.Bottom + 16;
                    panel.InvalidateVirtualHeight();
                };

            mlTextBox.LocationChanged += (s, e) =>
                {
                    single2.Y = mlTextBox.Bottom + 16;
                    panel.InvalidateVirtualHeight();
                };


            Toolbar myToolbar = Toolbar as Toolbar;
            myToolbar.IsMenuButtonVisible = true;
            myToolbar.MenuItems.Add(new MenuItem { Text = "Lorem Ipsum" });
            myToolbar.MenuItems.Add(new MenuItem { Text = "Single Line" });
            myToolbar.MenuItems.Add(new MenuItem { Text = "Line Numbers 1..200" });
            myToolbar.MenuItems.Add(new MenuItem { Text = "Line Numbers 1..10" });
            myToolbar.MenuItems.Add(new MenuItem { Text = "Exit" });

            myToolbar.MenuItems[0].Tap += (s, e) => mlTextBox.Text = lorem;
            myToolbar.MenuItems[1].Tap += (s, e) => mlTextBox.Text = "Single Line";
            myToolbar.MenuItems[2].Tap += (s, e) =>
                {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 1; i < 200; i++)
                    {
                        sb.Append(i);
                        sb.Append('\n');
                    }
                    mlTextBox.Text = sb.ToString();
                };

            myToolbar.MenuItems[3].Tap += (s, e) =>
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 1; i < 10; i++)
                {
                    sb.Append(i);
                    sb.Append('\n');
                }
                mlTextBox.Text = sb.ToString();
            };
            myToolbar.MenuItems[4].Tap += (s, e) => Hide();


            myToolbar.MenuItems[4].Tap += (s, e) => Hide();


            Label l = new Label
            {
                Text = "Multiline Textbox",
                Alignment = Alignment.Center,
                Height = 64,
                LineAlignment = Alignment.Center,
                Font = MainPage.boldFont,
                TextColor = Color.Gold
            };
            l.Bounds = new Rectangle(0, 0, Screen.Width, 64);
            Controls.Add(l);

            textBox = mlTextBox;
            panel.Controls.Add(single1, mlTextBox, single2);
            Controls.Add(panel);

            this.Color = Color.Black;
            textBox.Text = copyPaste;

        }

        protected override void OnInitialized()
        {
            base.OnInitialized();
        }
    }
}
